﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using PlatformNetCore.Common;
using PlatformNetCore.Common.Helper;
using PlatformNetCore.Entity;
using PlatformNetCore.IService;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PlatformNetCore.Web.Models
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class AuthAttribute : ActionFilterAttribute, IAuthorizationFilter
    {
        public AuthAttribute()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="menuCode"></param>
        public AuthAttribute(string menuCode)
        {
            MenuCode = menuCode;
        }
        /// <summary>
        /// MenuCode
        /// </summary>
        public string MenuCode { get; private set; }
        /// <summary>
        /// 已使用到的变量参数(如不重新赋值，请勿覆盖)
        /// ViewBag.Title（标题）
        /// </summary>
        /// <param name="filterContext"></param>     
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                #region 顶部底部菜单
                //菜单
                var controller = filterContext.Controller as Controller;
                IMenuService menuService = IOCContainerManager.Resolve<IMenuService>();
                var list = menuService.GetMenuList(k => k.IsAvailable == 1 && k.IsDel == 0);
                List<Menu> menuList = list.Where(k => k.MenuType == MenuType.TopRight.ToString()).OrderBy(s => s.SortNo).ToList();
                controller.ViewBag.MenuTopRight = menuList;
                controller.ViewBag.MenuBottom = menuList;
                controller.ViewBag.MenuTopOther = list.Where(k => k.MenuType == MenuType.Other.ToString()).OrderBy(s => s.SortNo).ToList();

                //string path = filterContext.HttpContext.Request.Path.Value;
                controller.ViewBag.ParentCode = MenuCode;
                controller.ViewBag.MenuCode = MenuCode;
                var current = list.FirstOrDefault(k => k.MenuCode == MenuCode);
                if (current != null)
                {
                    controller.ViewBag.Title = current.Title;
                    controller.ViewBag.ParentTitle = current.Title;
                    if (current.Level == 2)
                    {
                        Menu parentMenu = list.FirstOrDefault(k => k.ID == current.ParentID);
                        if (parentMenu != null)
                        {
                            controller.ViewBag.ParentTitle = parentMenu.Title;
                            controller.ViewBag.ParentCode = parentMenu.MenuCode;
                        }
                    }
                    controller.ViewBag.MenuTopSecond = list.Where(k => k.MenuType == MenuType.TopRight.ToString() && k.Level == 2 && (k.ParentID == current.ID || k.ParentID == current.ParentID)).OrderBy(s => s.SortNo).ToList();
                }
                else
                {
                    controller.ViewBag.Title = "关于鼓润";
                    controller.ViewBag.MenuTopSecond = new List<Menu>();
                }

                //公司基本资料
                ICompanyService companyService = IOCContainerManager.Resolve<ICompanyService>();
                controller.ViewBag.CompanyInfo = companyService.GetCompany().data;
                #endregion
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filterContext"></param>     
        public virtual void OnAuthorization(AuthorizationFilterContext filterContext)
        {

        }

    }
}